package org.example.struct.array;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2024/1/22 10:14
 * @apiNote
 */

public class Test00383CanConstruct {

    public static boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) {
            return false;
        }
        Map<Character,Integer> map = new HashMap<>();
        for (char c : magazine.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        for(char c : ransomNote.toCharArray()){
            if(map.getOrDefault(c, 0) <= 0){
                return false;
            }else{
                map.put(c, map.get(c) - 1);
            }
        }
        return true;
    }
}
